import yyComponent from "../../Script/Common/yyComponent";
import { Node, _decorator, Vec3, v3, Mesh, utils, MeshRenderer, Material, RenderComponent, renderer, Vec4, v4 } from "cc";
import GlobalData from "../../Script/Common/GlobalData";
import { GlobalEnum } from "../../Script/GameSpecial/GlobalEnum";
import { EventType } from "../../Script/GameSpecial/GameEventType";

const { ccclass, property } = _decorator;
/**教学用，显示在地面的箭头，连接玩家和目的地，Y轴旋转为0时，指向X轴负方向 */
@ccclass
export default class TeachArrow extends yyComponent {

    @property(MeshRenderer)
    protected mesh: MeshRenderer = null;

    @property(MeshRenderer)
    protected mesh2: MeshRenderer = null;

    protected pass: renderer.Pass = null;
    protected handle: number = null;
    protected matLen: Vec4 = null;
    protected arrowWith: number = 0.6;
    protected initMat() {
        let w = this.arrowWith;
        let l = 20;
        let data = {
            positions: [
                -w, 0, 0,
                -w, 0, l,
                w, 0, l,
                w, 0, 0
            ],
            indices: [
                0, 1, 2,
                0, 2, 3
            ],
            uvs: [0, 0, 0, 1, 1, 1, 1, 0],
        }
        let mesh = utils.createMesh(data);
        this.mesh.mesh = mesh;

        w *= 3;
        this.arrowWith = w;
        let data2 = {
            positions: [
                0, 0, 0,
                -w, 0, w,
                w, 0, w
            ],
            indices: [0, 1, 2],
            uvs: [0.5, 0, 0, 1, 1, 1],
        };
        let mesh2 = utils.createMesh(data2);
        this.mesh2.mesh = mesh2;
        this.mesh.node.setPosition(w, 0.1, 0);

        let pass = this.mesh2.getMaterialInstance(0).passes[0];
        let handle = pass.getHandle("len");
        let len = v4();
        pass.getUniform(handle, len);
        len.y = w * 0.25;
        pass.setUniform(handle, len);

        this.pass = this.mesh.getMaterialInstance(0).passes[0];
        this.handle = this.pass.getHandle("len");
        this.matLen = v4();
        this.pass.getUniform(this.handle, this.matLen);
        this.matLen.y = w * 0.25;
        this.applyMat();
    }
    protected applyMat() {
        this.pass.setUniform(this.handle, this.matLen);
    }

    protected initSub() {
        this.initMat();
        this.initStartNode();
    }

    protected onEvents() {
        this.on(EventType.myTeachEvent.setArrowTargetPos, this.setTargetPos, this);
        this.on(EventType.myTeachEvent.hideArrow, this.onHideArrow, this);
        this.on(EventType.Common.TeachEvents.teachFinish, this.onTeachFinish, this);
    }

    protected startNode: Node = null;
    protected startPos: Vec3 = null;
    protected targetPos: Vec3 = null;
    protected initStartNode() {
        this.startNode = GlobalData.get(GlobalEnum.myGlobalDataType.player).node;
        this.startPos = v3();
        this.targetPos = v3();
    }
    public setTargetPos(pos: Vec3) {
        this.targetPos.set(pos);
        this.node.setPosition(pos);
        this.node.active = true;
    }

    protected setStartPos(pos: Vec3) {
        let x = pos.x - this.targetPos.x;
        let z = this.targetPos.z - pos.z;
        let r = Math.atan2(z, x) * 0.5;
        this.node.setRotation(0, Math.sin(r), 0, Math.cos(r));
        let l = Math.sqrt(x * x + z * z);
        this.setLen(l);
    }
    //箭头长度
    protected setLen(l) {
        l -= this.arrowWith;
        if (l < 0) l = 0;
        this.matLen.x = l;
        this.applyMat();
    }
    protected onHideArrow() {
        this.node.active = false;
    }

    protected onTeachFinish() {
        this.offEvents();
        this.node.destroy();
    }



    update(dt: number) {
        this.startNode.getPosition(this.startPos);
        this.setStartPos(this.startPos);
    }

}
